package com.taotao.search.service.impl;

import java.awt.Color;

import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.SearchResult;
import com.taotao.search.service.SearchService;
import com.taotao.search.service.dao.SearchDao;

@Service
public class SearchServiceImpl implements SearchService{

	@Autowired
	private SearchDao searchDao;
	
	@Override
	public SearchResult search(String queryString, Integer page, Integer rows) throws Exception {
		// 封装查询条件
		SolrQuery query = new SolrQuery();
		//设置条件
		query.setQuery(queryString);
		//设置默认域
		query.set("df", "item_title");
		//分页
		if(page == null && page < 0 ){
			page = 1;
		}
		query.setStart((page -1 )*rows);
		query.setRows(rows);
		//开启高亮
		query.setHighlight(true);
		//添加高亮域
		query.addHighlightField("item_title");
		//设置高亮的头和尾
		query.setHighlightSimplePre("<em style=\"color:red\">");
		query.setHighlightSimplePost("</em>");
		//调用dao
		SearchResult searchResult = searchDao.search(query);
		//设置总页数
		Long totalPages = null;
		if(searchResult.getRecourdCount()%rows == 0){
			totalPages = searchResult.getRecourdCount()/rows;
		}else{
			totalPages = searchResult.getRecourdCount()/rows + 1;
		}
		searchResult.setTotalPages(totalPages);
		
		return searchResult;
	}

}
